home *** CD-ROM | disk | FTP | other *** search
- Path: zippy.cais.net!news
- From: mgeiger@mail.drsystems.com (Mark Geiger)
- Newsgroups: comp.lang.c
- Subject: Re: Command line Arguments
- Date: Tue, 06 Feb 1996 02:59:25 GMT
- Organization: Capital Area Internet Service info@cais.com 703-448-4470
- Message-ID: <4f6jpb$q7h@zippy.cais.net>
- References: <4f2qev$9jq@cloner3.netcom.com>
- Reply-To: mgeiger@mail.drsystems.com
- NNTP-Posting-Host: 205.252.35.151
- X-Newsreader: Forte Free Agent 1.0.82
-
- buxx@ix.netcom.com(Glen 'Steve' Vandiver ) wrote:
-
- >Hi! I have been haveing trouble with my commandline arguements. I have
- >it too where i can read the entire argument string after the run. No
- >prob. but lets say i want it to split it up like this. the first word
- >goes into char *user; and the rest goes into char *command;. HOW WOULD
- >I DO THIS? thanx! bye!
-
- Here is a solution. Sorry, but I haven't compiled it...there might be
- a typo here or there or maybe even a missing header file...but
- hopefully you'll get the idea <g>
-
- #include <malloc.h>
- #include <string.h>
-
- int main(int argc, char *argv[])
- {
- char *user;
- char *command;
- int user_len;
- int command_len;
- int i;
-
- user = NULL;
- command = NULL;
- user_len = 0;
- command_len = 0;
-
- /* Make sure that there's at least one arg to play with */
-
- if ( argc <= 1 )
- return -1;
-
- /* Find out how much memory we need to allocate to hold */
- /* the user name */
-
- user_len = strlen(argv[1]);
-
- /* Allocate memory for the user name, don't forget to allow for */
- /* the terminating nul */
-
- user = (char *) malloc(user_len + 1);
-
- if ( NULL == user ) {
- /* Handle no-memory error */
- return -1;
- }
-
- /* Store the user name */
-
- strcpy(user, argv[1]);
-
- /* Find out how much memory we'll need to store the remainder */
- /* Remember to allow room for a delimiting space character */
- /* between each arg */
-
- for ( i = 2; i < argc; i++ )
- command_len += ( 1 + strlen(argv[i]));
-
- if ( command_len ) {
-
- /* Allocate the memory we need to hold the remaining args */
-
- command = (char *) malloc(command_len + 1);
-
- if ( NULL == command ) {
- /* Handle no-memory error */
- free(user);
- return -1;
- }
-
- /* Now copy each arg over to our string */
- /* Use a space to separate them */
-
- command[0] = '\0';
- for ( i = 2; i < argc; i++ ) {
- strcat(command, " ");
- strcat(command, argv[i]);
- }
- }
-
- /* Do whatever with user and command */
-
- /* Clean up */
-
- if ( NULL != user )
- free(user);
-
- if ( NULL != command )
- free(command);
-
- return 0;
- }
-
-
-
-
-
-
-
-
-